{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 解读 `Span`\n",
    "\n",
    "## `SpanNode`\n",
    "\n",
    "```c++\n",
    "\n",
    "/*!\n",
    " * \\brief Span information for debugging purposes\n",
    " */\n",
    "class Span;\n",
    "/*!\n",
    " * \\brief Stores locations in frontend source that generated a node.\n",
    " */\n",
    "class SpanNode : public Object {\n",
    " public:\n",
    "  /*! \\brief The source name. */\n",
    "  SourceName source_name;\n",
    "  /*! \\brief The line number. */\n",
    "  int line;\n",
    "  /*! \\brief The column offset. */\n",
    "  int column;\n",
    "  /*! \\brief The end line number. */\n",
    "  int end_line;\n",
    "  /*! \\brief The end column number. */\n",
    "  int end_column;\n",
    "\n",
    "  // override attr visitor\n",
    "  void VisitAttrs(AttrVisitor* v) {\n",
    "    v->Visit(\"source_name\", &source_name);\n",
    "    v->Visit(\"line\", &line);\n",
    "    v->Visit(\"column\", &column);\n",
    "    v->Visit(\"end_line\", &end_line);\n",
    "    v->Visit(\"end_column\", &end_column);\n",
    "  }\n",
    "  static constexpr bool _type_has_method_sequal_reduce = true;\n",
    "\n",
    "  bool SEqualReduce(const SpanNode* other, SEqualReducer equal) const {\n",
    "    return equal(source_name, other->source_name) && equal(line, other->line) &&\n",
    "           equal(column, other->column) && equal(end_line, other->end_line) &&\n",
    "           equal(end_column, other->end_column);\n",
    "  }\n",
    "\n",
    "  static constexpr const char* _type_key = \"Span\";\n",
    "  TVM_DECLARE_BASE_OBJECT_INFO(SpanNode, Object);\n",
    "};\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`SpanNode` 继承自 `Object` 类的子类，它表示生成节点的前端源代码中的位置信息。该类具有以下成员变量和成员函数：\n",
    "\n",
    "- `source_name`：`SourceName` 类型的成员变量，表示生成节点的源名称。\n",
    "- `line`：整数类型的成员变量，表示行号。\n",
    "- `column`：整数类型的成员变量，表示列偏移量。\n",
    "- `end_line`：整数类型的成员变量，表示结束行号。\n",
    "- `end_column`：整数类型的成员变量，表示结束列号。\n",
    "- `VisitAttrs(AttrVisitor* v)`：成员函数，用于访问属性。它接受指向 `AttrVisitor` 对象的指针作为参数，并依次调用 `v->Visit(\"source_name\", &source_name)`、`v->Visit(\"line\", &line)`、`v->Visit(\"column\", &column)`、`v->Visit(\"end_line\", &end_line)` 和 `v->Visit(\"end_column\", &end_column)` 来访问相应的属性。\n",
    "- `_type_has_method_sequal_reduce`：静态常量表达式布尔值，表示该类型是否具有 `SEqualReduce` 方法。\n",
    "- `SEqualReduce(const SpanNode* other, SEqualReducer equal) const`：成员函数，用于比较两个 `SpanNode` 对象是否相等。它接受一个指向另一个 `SpanNode` 对象的指针和一个 `SEqualReducer` 对象作为参数，并使用 `equal` 函数比较两个对象的相应属性是否相等。\n",
    "- `_type_key`：静态常量字符指针，表示该类型的键。\n",
    "- `TVM_DECLARE_BASE_OBJECT_INFO(SpanNode, Object)`：宏定义，用于声明 `SpanNode` 对象的基本信息。\n",
    "\n",
    "总体来说，这段代码定义了表示生成节点位置信息的类，并提供了相应的属性访问和相等性比较功能。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `Span`\n",
    "\n",
    "```c++\n",
    "class Span : public ObjectRef {\n",
    " public:\n",
    "  TVM_DLL Span(SourceName source_name, int line, int end_line, int column, int end_column);\n",
    "\n",
    "  /*! \\brief Merge two spans into one which captures the combined regions. */\n",
    "  TVM_DLL Span Merge(const Span& other) const;\n",
    "\n",
    "  TVM_DEFINE_OBJECT_REF_METHODS(Span, ObjectRef, SpanNode);\n",
    "};\n",
    "```\n",
    "\n",
    "`Span` 类继承自 `ObjectRef` 类。该类具有以下成员函数：\n",
    "\n",
    "- `Span(SourceName source_name, int line, int end_line, int column, int end_column)`：一个构造函数，用于创建 `Span` 对象。它接受五个参数：`source_name` 表示源名称，`line` 表示起始行号， `end_line` 表示结束行号，`column` 表示起始列号，`end_column` 表示结束列号。\n",
    "- `Merge(const Span& other) const`：成员函数，用于合并两个 `Span` 对象为一个捕获了组合区域的 `Span` 对象。它接受一个指向另一个 `Span` 对象的常量引用作为参数，并返回一个新的 `Span` 对象。\n",
    "- `TVM_DEFINE_OBJECT_REF_METHODS(Span, ObjectRef, SpanNode)`：宏定义，用于定义 `Span` 对象的引用方法。\n",
    "\n",
    "总体来说，这段代码定义了一个用于表示源代码片段位置信息的类，并提供了相应的构造函数和合并功能。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `SequentialSpanNode`\n",
    "\n",
    "```c++\n",
    "/*!\n",
    " * \\brief Store a list of spans for an expr generated from mulitple source exprs\n",
    " */\n",
    "class SequentialSpanNode : public SpanNode {\n",
    " public:\n",
    "  /*! \\brief The original source list of spans to construct a sequential span. */\n",
    "  Array<Span> spans;\n",
    "\n",
    "  // override attr visitor\n",
    "  void VisitAttrs(AttrVisitor* v) {\n",
    "    SpanNode::VisitAttrs(v);\n",
    "    v->Visit(\"spans\", &spans);\n",
    "  }\n",
    "\n",
    "  static constexpr const char* _type_key = \"SequentialSpan\";\n",
    "  TVM_DECLARE_FINAL_OBJECT_INFO(SequentialSpanNode, SpanNode);\n",
    "\n",
    "  bool SEqualReduce(const SequentialSpanNode* other, SEqualReducer equal) const {\n",
    "    if (spans.size() != other->spans.size()) {\n",
    "      return false;\n",
    "    }\n",
    "\n",
    "    for (size_t i = 0, e = spans.size(); i != e; ++i) {\n",
    "      if (!StructuralEqual()(spans[i], other->spans[i])) {\n",
    "        return false;\n",
    "      }\n",
    "    }\n",
    "    return true;\n",
    "  }\n",
    "};\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`SequentialSpanNode` 类继承自 `SpanNode` 类。该类具有以下成员变量和成员函数：\n",
    "\n",
    "- `spans`：`Array<Span>` 类型的成员变量，表示原始 source exprs 列表中的 span，用于构造顺序 span。\n",
    "- `VisitAttrs(AttrVisitor* v)`：一个成员函数，用于访问属性。它接受一个指向`AttrVisitor`对象的指针作为参数，并首先调用基类的`VisitAttrs`函数，然后调用`v->Visit(\"spans\", &spans)`来访问`spans`属性。\n",
    "- `_type_key`：一个静态常量字符指针，表示该类型的键。\n",
    "- `TVM_DECLARE_FINAL_OBJECT_INFO(SequentialSpanNode, SpanNode)`：一个宏定义，用于声明`SequentialSpanNode`对象的最终信息。\n",
    "- `SEqualReduce(const SequentialSpanNode* other, SEqualReducer equal) const`：一个成员函数，用于比较两个`SequentialSpanNode`对象是否相等。它接受一个指向另一个`SequentialSpanNode`对象的指针和一个`SEqualReducer`对象作为参数，并使用`equal`函数比较两个对象的`spans`属性是否相等。如果长度不相等或任何一个元素不相等，则返回`false`；否则返回`true`。\n",
    "\n",
    "总体来说，这段代码定义了一个用于存储由多个源表达式生成的表达式的顺序跨度列表的类，并提供了相应的属性访问和相等性比较功能。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `SequentialSpan`\n",
    "\n",
    "```c++\n",
    "/*!\n",
    " * \\brief Reference class of SequentialSpanNode.\n",
    " * \\sa SequentialSpanNode\n",
    " */\n",
    "class SequentialSpan : public Span {\n",
    " public:\n",
    "  TVM_DLL SequentialSpan(Array<Span> spans);\n",
    "\n",
    "  TVM_DLL SequentialSpan(std::initializer_list<Span> init);\n",
    "\n",
    "  TVM_DEFINE_OBJECT_REF_METHODS(SequentialSpan, Span, SequentialSpanNode);\n",
    "};\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`SequentialSpan` 类继承自 `Span` 类。该类具有以下成员函数：\n",
    "\n",
    "- `SequentialSpan(Array<Span> spans)`：一个构造函数，用于创建一个`SequentialSpan`对象。它接受一个`Array<Span>`类型的参数`spans`，表示顺序跨度列表。\n",
    "- `SequentialSpan(std::initializer_list<Span> init)`：另一个构造函数，用于创建一个`SequentialSpan`对象。它接受一个`std::initializer_list<Span>`类型的参数`init`，表示顺序跨度列表。\n",
    "- `TVM_DEFINE_OBJECT_REF_METHODS(SequentialSpan, Span, SequentialSpanNode)`：一个宏定义，用于定义`SequentialSpan`对象的引用方法。\n",
    "\n",
    "总体来说，这段代码定义了一个用于表示顺序跨度列表的类，并提供了相应的构造函数和引用方法。"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
